﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccess;
namespace QLDSVCNTT_Nhom1
{
    public partial class frmQuanLyPCGD : Form
    {
        public frmQuanLyPCGD()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            //timer_PhanCongGiangDay.Enabled = false;
            this.Close();
            
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (lvPCGD.SelectedItems.Count >0)
            {
                PhanCongGiangDay pc = new PhanCongGiangDay();
                GetPCGD(pc);
                if (PhanCongGiangDay.Delete(pc) > 0 )
                {
                    MessageBox.Show("Xóa thành công", "Thông báo");
                    LoadDanhSachPCGD();
                }
                else
                {
                    MessageBox.Show("Xóa không thành công");
                }
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn gì để xóa, vui lòng chọn lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void GetPCGD(PhanCongGiangDay pc)
        {
            pc.MaGV = lvPCGD.SelectedItems[0].SubItems[0].Text;
            pc.MaMH = lvPCGD.SelectedItems[0].SubItems[1].Text;
            pc.HocKy =Convert.ToInt32(lvPCGD.SelectedItems[0].SubItems[2].Text);
            pc.NamHoc = lvPCGD.SelectedItems[0].SubItems[3].Text;
        }

        private void frmQuanLyPCGD_Load(object sender, EventArgs e)
        {
            LoadDanhSachPCGD();
            //timer_PhanCongGiangDay.Enabled = true;
            
        }

        private void LoadDanhSachPCGD()
        {
            List<PhanCongGiangDay> list = PhanCongGiangDay.All();
            lvPCGD.Items.Clear();
            int i = 1;
            foreach (PhanCongGiangDay pc in list)
            {
                ListViewItem item = lvPCGD.Items.Add(i.ToString());
                item.SubItems.Add(pc.MaGV.ToString());
                item.SubItems.Add(pc.TemGV.ToString());
                item.SubItems.Add(pc.MaMH.ToString());
                item.SubItems.Add(pc.TenMH.ToString());
                item.SubItems.Add(pc.HocKy.ToString());
                item.SubItems.Add(pc.NamHoc.ToString());
                i++;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            FrmThemPCGD frm = new FrmThemPCGD();
            frm.ShowDialog();
            LoadDanhSachPCGD();
        }

        private void timer_PhanCongGiangDay_Tick(object sender, EventArgs e)
        {
            if (lvPCGD.SelectedItems.Count >0)
            {
                return;
            }
            else
            {
                LoadDanhSachPCGD();   
            }
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            ListView root = lvPCGD;
            string name = "DanhSachPhanCongGiangDay";
            saveFileDialog.Filter = "Save File(*.xls)|*.xls";
            saveFileDialog.FileName = "";
            saveFileDialog.Title = "Save to...";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                int num = SaveListviewToExcel.Export2Excel(root, System.IO.Path.GetFullPath(saveFileDialog.FileName), name);
                if (num == 1)
                {
                    MessageBox.Show("luu thanh cong");
                }
                else
                {
                    MessageBox.Show("Luu khong thanh cong");
                }
            }
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadDanhSachPCGD();
            //timer_PhanCongGiangDay.Enabled = true;
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnXoa.PerformClick();
        }

        private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnThem.PerformClick();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            if (txtFind.Text != "" && cbbFind.SelectedItem != null)
            {
                //timer_Refresh.Enabled = false;
                string chuoitim = txtFind.Text;
                string chon = cbbFind.SelectedItem.ToString();
                TimKiem(chuoitim, chon);
            }
            else
            {
                MessageBox.Show("bạn chua nhập gì để tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void TimKiem(string chuoitim, string chon)
        {
            List<PhanCongGiangDay> list = null;
            switch (chon)
            {
                case "Mã Giáo Viên":
                    lvPCGD.Items.Clear();
                    list = PhanCongGiangDay.FindByMaGV(chuoitim);
                    LoadListView(list);
                    break;
                case "Tên Giáo Viên":
                    lvPCGD.Items.Clear();
                    list = PhanCongGiangDay.FindByTenGV(chuoitim);
                    LoadListView(list);
                    break;
                case "Mã Môn Học":
                    lvPCGD.Items.Clear();
                    list = PhanCongGiangDay.FindByMaMH(chuoitim);
                    LoadListView(list);
                    break;
                case "Tên Môn Học":
                    lvPCGD.Items.Clear();
                    list = PhanCongGiangDay.FindByTenMH(chuoitim);
                    LoadListView(list);
                    break;
                case "Học Kỳ":
                    lvPCGD.Items.Clear();
                    list = PhanCongGiangDay.FindByHocKy(chuoitim);
                    LoadListView(list);
                    break;
                case "Năm Học":
                    lvPCGD.Items.Clear();
                    list = PhanCongGiangDay.FindByNamHoc(chuoitim);
                    LoadListView(list);
                    break;
                default:
                    this.btnFind.PerformClick();
                    break;
            }
        }

        private void LoadListView(List<PhanCongGiangDay> list)
        {
            lvPCGD.Items.Clear();
            int i = 1;
            foreach (PhanCongGiangDay item in list)
            {
                LayListView(item, i);
                i++;
            }
        }

        private void LayListView(PhanCongGiangDay item, int i)
        {
            ListViewItem lv = lvPCGD.Items.Add(i.ToString());
            lv.SubItems.Add(item.MaGV.ToString());
            lv.SubItems.Add(item.TemGV.ToString());
            lv.SubItems.Add(item.MaMH.ToString());
            lv.SubItems.Add(item.TenMH.ToString());
            lv.SubItems.Add(item.HocKy.ToString());
            lv.SubItems.Add(item.NamHoc.ToString());
        }

        private void txtFind_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt16(e.KeyChar) == 13)
            {
                btnFind.PerformClick();
            }
        }
    }
}
